let { api } = require("./../../utils/api.js");
let uploadImgs = [];
Page({
  data: {
    categoryindex: 1,
    dishName: "",
    dishCategory: 1,
    dishIntroduction: "",
    typeArr: ["荤菜", "素菜", "点心", "水果"],
    Images: [] // 上传的照片
  },
  // 获取选择的菜单分类
  bindPickerChange: function(e) {
    console.log(Number(e.detail.value));
    this.setData({
      categoryindex: Number(e.detail.value)
    });
  },
  // 点击图片进行放大
  imgPreview: function(e) {
    let src = e.currentTarget.dataset.src;
    let srcArr = [src];
    let current = src;
    if (Object.prototype.toString.call(src) == "[object Array]") {
      srcArr = src;
      current = e.currentTarget.dataset.current;
    }
    wx.previewImage({
      current,
      urls: srcArr // 需要预览的图片http链接列表
    });
  },
  // 选择图片上传
  chooseImages: function() {
    const that = this;
    wx.chooseImage({
      count: 6, //最多6张
      sourceType: ['album', 'camera'],
      success: function(res) {
        console.log(res.tempFilePaths);
        that.setData({
          Images: res.tempFilePaths
        });
      },
      fail: function(res) {
        console.log(res);
      },
      complete: function(res) {}
    });
  },
  // 校验输入
  checkData: function(e) {
    let toast = "";
    let bool = false;
    if (!e.detail.value.dishName) {
      toast = "菜名不能为空！";
    } else if (!e.detail.value.dishIntroduction) {
      toast = "营养介绍不能为空！";
    } else if (this.data.Images.length === 0) {
      toast = "请上传菜系图片！";
    } else {
      bool = true;
    }
    toast &&
      wx.showToast({
        title: toast,
        icon: "none",
        duration: 2000
      });
    return bool;
  },
  // 新增菜系
  addCuisine: function() {
    let that = this;
    let { dishName, dishCategory, dishIntroduction } = this.data;
    let obj = {
      dishName,
      dishCategory,
      dishIntroduction,
      photolist: uploadImgs
    };
    console.log(obj);
    wx.request({
      url: api + "addDish.do",
      method: "POST",
      data: obj,
      success: function(res) {
        console.log(res.data);
        // 关闭上传图片loading的提示框
        wx.hideLoading();
        if (res.data.status === 1) {
          wx.showToast({
            title: "新增成功",
            icon: "success",
            duration: 2000
          });
          that.setData({
            dishName: "",
            categoryindex: 1,
            dishIntroduction: "",
            Images: ""
          });
          // 清空之前填写的数据
          uploadImgs = [];
        } else {
          wx.showToast({
            title: "新增失败，请稍后重试!",
            icon: "none",
            duration: 2000
          });
        }
      },
      fail: function(res) {
        wx.showToast({
          title: "网络错误，请稍后重试",
          icon: "none",
          duration: 2000
        });
      }
    });
  },
  //上传服务器图片，获取相应的路径
  uploadDIY: function(filePaths, i, length) {
    let that = this;
    wx.uploadFile({
      url: api + "upload.do",
      filePath: filePaths[i],
      name: "fileData",
      success: res => {
        let newRes = JSON.parse(res.data);
        if (newRes.status === 1) {
          uploadImgs.push(newRes.data[0]);
        }
        if (i === length - 1) {
          //上传所有评价信息
          that.addCuisine();
        }
      },
      fail: res => {
        wx.showToast({
          title: "图片上传失败，请重试!",
          icon: "none",
          duration: 2000
        });
      },
      complete: () => {
        i++;
        if (i !== length) {
          //递归调用uploadDIY函数
          that.uploadDIY(filePaths, i, length);
        }
      }
    });
  },
  //点击确认新增
  formSubmit: function(e) {
    let { Images } = this.data;
    let {
      dishName,
      dishCategory,
      categoryindex,
      dishIntroduction
    } = e.detail.value;
    console.log("form发生了submit事件，携带数据为：", e.detail.value);
    if (this.checkData(e)) {
      console.log("信息无误！");
      this.setData({
        dishName,
        dishCategory: categoryindex + 1,
        dishIntroduction
      });
      uploadImgs = [];
      wx.showLoading({
        title: "图片上传中!"
      });
      this.uploadDIY(Images, 0, Images.length);
    }
  },
  //进入发布菜单页面
  gotoRelease: function() {
    wx.navigateTo({
      url: "./../publishMenu/publishMenu"
    });
  }
});
